//用户相关的仓库
import { defineStore } from 'pinia'
import { reqLogin, reqInfo, reqlogout } from '@/api/user/index.ts'
import { ElMessage } from 'element-plus'
import { constanceRoute } from '@/router/routes'
import { localRemove,localGet } from '@/utils/utils'

const useUserStore = defineStore('User', {
  //小仓库存储数据的地方
  state: () => {
    return {
      token: localGet("TOKEN"),
      menuRoutes: constanceRoute, //生层菜单的数组
      name:"",
      username: '',
      gender: '',
      introduction: '',
      avatar: '',
      role: '',
    }
  },
  actions: {
    //登录
    async userLogin(data: any) {
      const res = await reqLogin(data)
      if (res.ActionType == 'OK') {
        return 'ok'
      } else {
        ElMessage.warning(`${res.error}`)
        return Promise.reject(new Error(res.error))
      }
    },
    //登录返回数据
    async userInfo() {
      const res = await reqInfo()
      if (res.code == 200) {
        this.username = res.data.username
        this.name = res.data.name
        this.gender = res.data.gender
        this.introduction = res.data.introduction
        this.avatar = res.data.avatar
        this.role = res.data.role
        console.log(res)
        return 'ok'
      } else {
        return Promise.reject(new Error(res.error))
      }
    },
    //退出登录
    async logout() {
      const res = await reqlogout()
      if (res.code == 200) {
        this.token = ''
        this.username = ''
        this.name = ''
        this.gender = ''
        this.introduction = ''
        this.avatar = ''
        this.role = ''
        localRemove('TOKEN')
      }
    },
  },
  getters: {},
})

export default useUserStore
